<?php defined('BASEPATH') or die("No access");
    class User_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all user
        public function getAll()
        {
            //$this->db->order_by('user.user_id','asc');
            $this->db->order_by('user.user_order','asc');
            $this->db->from('user');
            $query=$this->db->get();
            return $query->result(); 
        }
        //get user by id 
        public function getUserById($id)
        {
            $this->db->where('user.user_id',$id);
            $this->db->from('user');
            $query=$this->db->get();
            return $query->row();
        }
        public function getUserForLogin($username,$password)
        {
            //$password=md5(md5(md5($password)));
            $this->db->where('user_name',$username);
            $this->db->where('user_password',$password);
            $query=$this->db->get('user');
            return $query->row();
            
        }
        
        // insert or update 
        public function save($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                $this->db->insert('user',$data);
            }
            else
            {
                $this->db->where('user_id',$id);
                $this->db->update('user',$data);
            }
            return $this->db->affected_rows();
        }
        
        //save order
        public function saveorder($id=array(),$order=array())
        {
            for($i=0;$i<count($id);$i++)
            {
                $this->db->where('user_id',$id[$i]);
                $this->db->update('user',array('user_order'=>$order[$i]));
            }
        }
        //delete array users
        public function delete($id=array())
        {
            $this->db->where_in('user_id',$id);
            $this->db->delete('user');
            return $this->db->affected_rows();
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('user_id',$id);
            if($status==0)
            {
                $this->db->update('user',array('user_publish'=>0));
            }
            else
            {
                $this->db->update('user',array('user_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

